package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Transactional
@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Resource
    private WmMaterialMapper wmMaterialMapper;
    @Resource
    private FileStorageService fileStorageService;

    /**
     * 图片上传
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        if(multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String fileUrl = "";
        //获取文件后缀名
        String originalFilename = multipartFile.getOriginalFilename();
        if(StringUtils.hasText(originalFilename)){
            String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));

            //保存图片到minio
            String fileName = UUID.randomUUID().toString().replace("-", "");
            try {
                fileUrl = fileStorageService.uploadImgFile("",fileName + postfix, multipartFile.getInputStream());
                log.info("图片上传成功");
            } catch (IOException e) {
                e.printStackTrace();
                log.error("WmMaterialServiceImpl图片上传失败");
            }
        }
        //保存到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(fileUrl);
        wmMaterial.setIsCollection((short)0);//默认0不收藏 1是收藏
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        this.save(wmMaterial);

        //4.返回结果

        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 素材列表查询
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto wmMaterialDto) {
        //校验参数
        wmMaterialDto.checkParam();
        //2.分页查询
        IPage<WmMaterial> page = new Page<>(wmMaterialDto.getPage(),wmMaterialDto.getSize());
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //是否收藏
        if(wmMaterialDto.getIsCollection() != null && wmMaterialDto.getIsCollection() == 1){
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        }
        //按照用户查询
        lambdaQueryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        //按照时间倒序
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        //开始分页查询
        IPage<WmMaterial> wmMaterialIPage = this.page(page, lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setData(wmMaterialIPage.getRecords());
        pageResponseResult.setSize((int)wmMaterialIPage.getSize());
        pageResponseResult.setCurrentPage(wmMaterialDto.getPage());
        pageResponseResult.setTotal((int)wmMaterialIPage.getTotal());
        return ResponseResult.okResult(pageResponseResult);
    }
}
